package com.example.demo.controller;

import com.example.demo.model.BlogInfo;
import com.example.demo.service.BlogService;
import com.example.demo.utils.JwtUtils;
import io.jsonwebtoken.lang.Assert;
import jakarta.servlet.http.HttpServletRequest;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import java.util.List;

@RestController
@Slf4j
@RequestMapping("/blog")
public class BlogController {
    @Autowired
    private BlogService blogService;
    @RequestMapping("/getList")
    public List<BlogInfo> getList(){
        return blogService.getList();

    }

    @RequestMapping("/getBlogDetail")
    public BlogInfo getBlogDetail(Integer blogId,HttpServletRequest request){
        //从token 中获取 用户id
        String userToken = request.getHeader("user_token");
        Integer userId = JwtUtils.getUserIdFromToken(userToken);
        //从博客信息中 获取作者id


        BlogInfo blogInfo = blogService.getBlogDetail(blogId);
        Integer authorId = blogInfo.getUserId();
        if(userId!=null && userId == authorId){
            blogInfo.setIsLoginUser(true);
        }

        return blogInfo;

    }

    /**
     * 发布博客
     */
    @RequestMapping("/add")
    public Boolean addBlog(@Validated @RequestBody BlogInfo blogInfo, HttpServletRequest request){
        log.info("addBlog 接收参数: "+ blogInfo);
        String userToken = request.getHeader("user_token");
        Assert.notNull(userToken,"userToken 不能为null");
        Integer userId = JwtUtils.getUserIdFromToken(userToken);
        blogInfo.setUserId(userId);

        Integer result  = null;
        try{
            result = blogService.insertBlog(blogInfo);
            if(result == 1){
                return true;
            }
        }catch (Exception e){
            log.error("发布博客失败 !" , e);
        }
        return false;
    }

    /**
     * 删除博客
     */
    @RequestMapping("/deleteBlog")
    public Boolean deleteBlog(Integer blogId){
        log.info("deleteBlog 接收参数: "+ blogId);
        Assert.notNull(blogId,"blogId 不能为null");
       Integer result = null;
       try{
           result = blogService.deleteBlog(blogId);
           if(result>=1){
               return true;
           }
       }catch (Exception e){
           log.error("deleteBlog 异常:"+e);
       }
       return false;


    }

    /**
     * 跟新博客信息
     */
    @RequestMapping("updateBlog")
    public Boolean updateBlog(@Validated @RequestBody BlogInfo blogInfo,HttpServletRequest request){
//        String userToken = request.getHeader("user_token");
//        Assert.notNull(userToken,"userToken 不能为null");
//        Integer userId = JwtUtils.getUserIdFromToken(userToken);
        Integer result = null;
        try{
            result = blogService.updateBlog(blogInfo);
            if(result>=1){
                return true;
            }
        }catch (Exception e){
            log.error("更新博客异常:"+e);
        }

        return false;


    }
}

